﻿using MediatR;
using MS.BasicData.Infrastructure;
using MS.BasicData.Infrastructure.IBaseRepository;
using System.ComponentModel.DataAnnotations;

namespace MS.BasicData.Read.API.Applications.Roles
{
    public class DeleteRoleRequestHandler : IRequestHandler<DeleteRoleRequestModel, ApiRestult>
    {
        /// <summary>
        /// 角色仓储
        /// </summary>
        private readonly IRoleBaseRepository _roleRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="roleRepository"></param>
        public DeleteRoleRequestHandler(IRoleBaseRepository roleRepository)
        {
            _roleRepository = roleRepository;
        }

        public async Task<ApiRestult> Handle(DeleteRoleRequestModel request, CancellationToken cancellationToken)
        {
            ApiRestult restult = new ApiRestult
            {
                isSuc = true,
                Code = Restult.Success,
                Message = "删除成功"
            };

            var role = await _roleRepository.GetAll(a=>a.RoleId == request.RoleId);

            if (role == null)
            {
                restult.isSuc = false;
                restult.Code = Restult.Fail;
                restult.Message = "删除失败,角色信息不存在";
                return restult;
            }
            role.IsDeleted = true;
            await _roleRepository.UpdateAsync(role);
            return restult;
        }
    }


    public class DeleteRoleRequestModel : IRequest<ApiRestult>
    {
        /// <summary>
        /// 角色ID
        /// </summary>
        public int RoleId { get; set; }
    }
}
